package com.cskt.securityproject.controller;

import com.cskt.securityproject.entity.User;
import com.cskt.securityproject.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.security.authentication.RememberMeAuthenticationProvider;
import org.springframework.security.authentication.RememberMeAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @description 用户控制器
 * @date 2023/12/14 13:39
 * @auth mo
 */
@Controller
@RequestMapping(value = "/user")
public class UserController {

    @Resource
    private UserService userService;

    @GetMapping(value = "/findAll")
    public String findAll(Model model) {
        //判断当前登录用户是否来源于记住我
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (RememberMeAuthenticationToken.class.isAssignableFrom(authentication.getClass())) {
            //判断当前的认证是不是通过rememberme得到的，如果是，则返回true
            throw new RememberMeAuthenticationException("认证信息来自于remeberme，请重新登录");
        }
        List<User> users = userService.findAll();
        model.addAttribute("userList", users);
        return "user_list";
    }

    @GetMapping(value = "/hello")
    @ResponseBody
    public String hello() {
        return "hello";
    }







}
